package com.lighting.common.log;

import lombok.NonNull;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;

/**
 * 异步线程池，带有MDC的那种
 */
public class MdcThreadPoolTaskExecutor extends ThreadPoolTaskExecutor {


    public MdcThreadPoolTaskExecutor() {
    }

    /**
     * @param corePoolSize
     * @param maxPoolSize
     * @param keepAliveTime
     * @param queueCapacity
     * @param poolNamePrefix
     */
    public MdcThreadPoolTaskExecutor(int corePoolSize, int maxPoolSize, int keepAliveTime, int queueCapacity, String poolNamePrefix) {
        setCorePoolSize(corePoolSize);
        setMaxPoolSize(maxPoolSize);
        setKeepAliveSeconds(keepAliveTime);
        setQueueCapacity(queueCapacity);
        setThreadNamePrefix(poolNamePrefix);
    }

    @Override
    public void execute(@NonNull Runnable command) {
        super.execute(new MdcRunnable(command));
    }

    @NonNull
    @Override
    public Future<?> submit(@NonNull Runnable task) {
        return super.submit(new MdcRunnable(task));
    }

    @NonNull
    @Override
    public <T> Future<T> submit(@NonNull Callable<T> task) {
        return super.submit(new MdcCallable(task));
    }
}